import mockConfig from './config/index';
import { sendSuccess, sendFail } from "./utils";
import { getList, addData, updateData, deleteData, batchDeleteData, findOne } from "./utils/readFile";

const prefix = `${mockConfig.prefix}/system`

export default [
  // 字典类型接口
  {
    url: `${prefix}/dict/type/list`,
    timeout: mockConfig.timeout,
    method: "get",
    response: (ctx) => {
      const data = getList('dict_type', ctx.query)
      return sendSuccess(data);
    },
  },
  {
    url: `${prefix}/dict/type`,
    timeout: mockConfig.timeout,
    method: "post",
    response: (ctx) => {
      const target = findOne('dict_type', ctx.body.dictType, 'dictType')
      if (target) {
        return sendFail({}, '字典类型已存在');
      }
      const data = addData('dict_type', ctx.body)
      return sendSuccess(data);
    },
  },
  {
    url: `${prefix}/dict/type`,
    timeout: mockConfig.timeout,
    method: "put",
    response: (ctx) => {
      const target = findOne('dict_type', ctx.body.dictType, 'dictType')
      if (target && target.id !== ctx.body.id) {
        return sendFail({}, '字典类型已存在');
      }
      const data = updateData('dict_type', ctx.body)
      return sendSuccess(data);
    },
  },
  {
    url: `${prefix}/dict/type`,
    timeout: mockConfig.timeout,
    method: "delete",
    response: (ctx) => {
      const { id } = ctx.body
      if (!Array.isArray(id)) {
        const data = deleteData('dict_type', id)
        return sendSuccess(data);
      } else {
        const data = batchDeleteData('table', id)
        return sendSuccess(data);
      }
    },
  },
  // 字典数据接口
  // 根据字典类型查找字典数据
  {
    url: `${prefix}/dict/data`,
    timeout: mockConfig.timeout,
    method: "get",
    response: (ctx) => {
      let { list } = getList('dict_data', ctx.query)
      return sendSuccess(list);
    },
  },
  {
    url: `${prefix}/dict/data/list`,
    timeout: mockConfig.timeout,
    method: "get",
    response: (ctx) => {
      const data = getList('dict_data', ctx.query)
      return sendSuccess(data);
    },
  },
  {
    url: `${prefix}/dict/data`,
    timeout: mockConfig.timeout,
    method: "post",
    response: (ctx) => {
      const { dictType, dictValue } = ctx.body
      const arr = getList('dict_data', { dictType })
      const target = arr.find(item => item.dictValue === dictValue)
      if (target) {
        return sendFail({}, '字典键值已存在');
      }
      const data = addData('dict_data', ctx.body)
      return sendSuccess(data);
    },
  },
  {
    url: `${prefix}/dict/data`,
    timeout: mockConfig.timeout,
    method: "put",
    response: (ctx) => {
      const { dictType, dictValue } = ctx.body
      const arr = getList('dict_data', { dictType })
      const target = arr.find(item => item.dictValue === dictValue)
      if (target && target.id !== ctx.body.id) {
        return sendFail({}, '字典键值已存在');
      }
      const data = updateData('dict_data', ctx.body)
      return sendSuccess(data);
    },
  },
  {
    url: `${prefix}/dict/data`,
    timeout: mockConfig.timeout,
    method: "delete",
    response: (ctx) => {
      const { id } = ctx.body
      if (!Array.isArray(id)) {
        const data = deleteData('dict_data', id)
        return sendSuccess(data);
      } else {
        const data = batchDeleteData('table', id)
        return sendSuccess(data);
      }
    },
  },
];
